package com.wendy.read.member.exception;


import com.wendy.read.common.utils.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){

        String eName = e.getClass().getName();
        if (eName.contains("DuplicateKeyException")){
            return Result.failure("当前数据已存在，请不要重复输入！");
        } else if (eName.contains("MethodArgumentNotValidException")) {
            return Result.failure("你的输入不合法，请检查输入的参数！");
        }

        System.out.println(eName);
        String error = StringUtils.hasLength(e.getMessage()) ? e.getMessage() : "操作失败，请联系管理员！";

        return Result.failure(error);
    }
}
